package com.example.controller;
import com.example.entity.Merchant;
import com.example.service.MerchantService;
import com.example.util.RejectRequest;
import com.example.util.Result;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@Tag(name = "merchant模块", description = "提交商家申请相关功能")
@RequestMapping("/merchant")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;

    // 提交认证申请
    @PostMapping("/apply")
    public Result<?> apply(@RequestBody Merchant merchant) {
        boolean success = merchantService.apply(merchant);
        return success ? Result.success("提交申请成功") : Result.fail("提交申请失败");
    }

    // 查询账号对应的认证状态
    @GetMapping("/status/{aId}")
    public Result<Merchant> getStatus(@PathVariable Long aId) {
        Merchant merchant = merchantService.getStatus(aId);
        return merchant != null ? Result.success("查询成功",merchant) : Result.fail("未找到对应认证信息");
    }

    // 审核通过
    @PostMapping("/approve/{mId}")
    public Result<?> approve(@PathVariable Long mId) {
        boolean success = merchantService.approve(mId);
        return success ? Result.success("审核操作成功") : Result.fail("审核操作失败");
    }

    // 审核拒绝
    @PostMapping("/reject")
    public Result<?> reject(@RequestBody RejectRequest request) {
    boolean success = merchantService.reject(request.getMId(), request.getReason());
    return success ? Result.success("审核拒绝操作成功") : Result.fail("审核拒绝操作失败");
    }

    // 获取所有待审核商家
    @GetMapping("/pending")
    public Result<List<Merchant>> listPending() {
        List<Merchant> list = merchantService.listPending();
        return Result.success(list);
    }

}
